package com.sbxBase.controller;


import com.common.entity.sbxBase.User;
import com.sbxBase.service.UserService;
import com.common.entity.Result;
import com.common.validations.NotRequiredField;
import com.common.validations.RequiredField;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "管理用户")
@RestController
@RequestMapping("/managerUser")
public class ManagerUserController {

    @Resource
    private UserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    public Result insert(@RequestBody @Validated(value = RequiredField.class) User user) {
        userService.insert(user);
        return Result.success();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable @ApiParam("用户id") Integer id) {
        userService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("用户信息修改")
    public Result update(@RequestBody @Validated(NotRequiredField.class) User user) {
        userService.managerUpdateUser(user);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询用户")
    public Result getUserPage(@Validated(value = NotRequiredField.class) User user,
                              @ApiParam("起始页") @RequestParam(defaultValue = "1") Integer pageIndex,
                              @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {

        return Result.page(userService.getUserPage(user, pageIndex, pageSize));
    }

}
